package cn.hj.pay.service.impl;

import cn.hj.basic.exception.BusinessException;
import cn.hj.basic.service.impl.BaseServiceImpl;
import cn.hj.order.service.IProductOrderService;
import cn.hj.pay.domain.AlipayInfo;
import cn.hj.pay.domain.PayBill;
import cn.hj.pay.mapper.AlipayInfoMapper;
import cn.hj.pay.mapper.PayBillMapper;
import cn.hj.pay.service.IPayBillService;
import cn.hj.pay.utils.AliPayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PayBillServiceImpl extends BaseServiceImpl<PayBill> implements IPayBillService {

    @Autowired
    private AlipayInfoMapper alipayInfoMapper;

    @Autowired
    private PayBillMapper payBillMapper;

    @Autowired
    private IProductOrderService productOrderService;

    @Override
    public String pay(PayBill billTmp) {
        if(billTmp==null){
            throw new BusinessException("使劲骂！！！");
        }
        PayBill Bill = payBillMapper.loadByUnionPaySn(billTmp.getUnionPaySn());
        if (Bill==null){
            throw new BusinessException("使劲骂！！！");
        }
        AlipayInfo alipayInfo = alipayInfoMapper.loadByShopId(Bill.getShop_id());
        Long payChannel = billTmp.getPayChannel();
        switch (payChannel.intValue()) {//0 支付宝  1  微信  2 银联
            case 0:{
               return AliPayUtils.pay(billTmp,alipayInfo);
            }
            case 1:{

            }
            case 2:{

            }
            default:break;
        }
        return null;
    }


    /**
     * 通过统一支付标识，查询统一支付单
     * @param unionPaySn
     * @return
     */
    @Override
    public PayBill loadByUnionPaySn(String unionPaySn) {
        return payBillMapper.loadByUnionPaySn(unionPaySn);
    }
}
